/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.dialog;

import java.awt.BorderLayout;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.SwingConstants;

import org.jxSwing.component.button.DeleteButton;
import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.dialog
 * @date 24/07/2014 - 21:07:08
 */
public abstract class ProgressDialog extends JWindow {
	
	private static final long serialVersionUID = 2924629882121336050L;
	
	private final String message;
	
	private JLabel lblMessage;
	
	private JProgressBar progressBar;
	
	private JPanel pnlProgress;
	
	private DeleteButton btnCancel;
	
	private final Thread thread;
	
	/**
	 * @param owner
	 * @param message
	 * @param enabledCancelButton
	 */
	public ProgressDialog(final Frame owner, final String message, final boolean enabledCancelButton) {
	
		super(owner);
		
		getOwner().setEnabled(false);
		
		this.message = message;
		
		initGUI();
		
		getBtnCancel().setVisible(enabledCancelButton);
		
		//
		thread = new Thread(new Runnable() {
			
			@Override
			public void run() {
			
				execute(ProgressDialog.this);
				
				getOwner().setEnabled(true);
				
				dispose();
			}
		});
	}
	
	private void initGUI() {
	
		setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
		getContentPane().add(getLblMessage(), BorderLayout.CENTER);
		getContentPane().add(getPnlProgress(), BorderLayout.SOUTH);
		setSize(750, 200);
		setLocationRelativeTo(null);
	}
	
	/**
	 * @param progressDialog
	 */
	public abstract void execute(ProgressDialog progressDialog);
	
	public DeleteButton getBtnCancel() {
	
		if(btnCancel == null) {
			btnCancel = new DeleteButton();
			btnCancel.setToolTipText(I18N.component().cancel());
			btnCancel.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					stop();
				}
			});
		}
		return btnCancel;
	}
	
	public JLabel getLblMessage() {
	
		if(lblMessage == null) {
			lblMessage = new JLabel(message);
			lblMessage.setFont(new Font("Courier New", Font.BOLD, 25));
			lblMessage.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblMessage;
	}
	
	public JPanel getPnlProgress() {
	
		if(pnlProgress == null) {
			pnlProgress = new JPanel();
			pnlProgress.setLayout(new BorderLayout(0, 0));
			pnlProgress.add(getProgressBar(), BorderLayout.CENTER);
			pnlProgress.add(getBtnCancel(), BorderLayout.EAST);
		}
		return pnlProgress;
	}
	
	public JProgressBar getProgressBar() {
	
		if(progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setIndeterminate(true);
		}
		return progressBar;
	}
	
	/**
	 * 
	 */
	public void setEnabledCancelButton(final boolean enabledCancelButton) {
	
		getBtnCancel().setVisible(enabledCancelButton);
	}
	
	/**
	 * 
	 */
	public void start() {
	
		thread.start();
		
		getOwner().setEnabled(false);
		
		setVisible(true);
	}
	
	/**
	 * 
	 */
	@SuppressWarnings("deprecation")
	public void stop() {
	
		//
		if(JOptionPane.showConfirmDialog(null, I18N.component().reallyWantToCancelThisOperation()) == JOptionPane.YES_OPTION) {
			
			thread.stop();
			
			getOwner().setEnabled(true);
			
			dispose();
		}
	}
}
